package com.liugh.controller.zhongche;

import com.baomidou.mybatisplus.plugins.Page;
import com.liugh.base.BusinessException;
import com.liugh.base.PublicResultConstant;
import com.liugh.config.ResponseModel;
import com.liugh.controller.BaseRestController;
import com.liugh.entity.standard.StandardAdvise;
import com.liugh.model.DownloadModel;
import com.liugh.service.zhongche.IStandardAdviseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Fatao on 2018/11/12 .
 */

@Api(description = "标准征求意见")
@RestController
@RequestMapping("/standard_advise")
public class StandardAdviseController extends BaseRestController {


    @Autowired
    private IStandardAdviseService standardAdviseService;


    /**
     * 添加一个标准征求意见 .
     * @param standardAdvise .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("添加标准征求意见")
    @PostMapping("/add")
    public ResponseModel<StandardAdvise> addAdvise(@RequestBody StandardAdvise standardAdvise) throws BusinessException {
        standardAdvise = standardAdviseService.addStandAdvise(standardAdvise);
        if (standardAdvise == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(standardAdvise);
        }
    }


    /**
     * 修改标准征求意见 .
     * @param standardAdvise .
     * @return .
     * @throws BusinessException .
     */
    @ApiOperation("修改标准征求意见")
    @PutMapping("/update")
    public ResponseModel<StandardAdvise> updateAdvise(@RequestBody StandardAdvise standardAdvise) throws BusinessException {
        standardAdvise = standardAdviseService.updateStandAdvise(standardAdvise);
        if (standardAdvise == null) {
            throw new BusinessException(PublicResultConstant.ERROR);
        } else {
            return fillSuccessData(standardAdvise);
        }
    }


    /**
     * 删除征求意见 .
     * @param id .
     * @return .
     */
    @ApiOperation("删除征求意见")
    @DeleteMapping("/delete")
    public ResponseModel<StandardAdvise> deleteAdvise(@RequestParam("id") int id) {
        int result = standardAdviseService.deleteStandAdvise(id);
        if (result > 0) {
            return fillSuccessData();
        } else {
            return fillFailData();
        }
    }


    /**
     * 根据id获取征求意见 .
     * @param id .
     * @return .
     */
    @ApiOperation("根据id获取征求意见")
    @GetMapping("/get")
    public ResponseModel<StandardAdvise> getAdviseById(@RequestParam("id") int id) {
        return fillSuccessData(standardAdviseService.getAdviseById(id));
    }


    /**
     * 分页获取所有的征求意见 .
     * @param pageNum .
     * @param pageSize .
     * @return .
     */
    @ApiOperation("分页获取所有的征求意见")
    @GetMapping("/get_all")
    public ResponseModel<Page<StandardAdvise>> getAllAdvise(@RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
                                                            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize) {
        return fillSuccessData(standardAdviseService.getAllStandAdvise(new Page<>(pageNum, pageSize)));
    }


    @GetMapping("/download")
    @ApiOperation(value = "返回附件的下载地址及原始文件名",notes = "返回的是一个list，第一个元素是空表格，第二个元素是填满意见的表格")
    public ResponseModel<List<DownloadModel>> downAttachmentEmpty(@RequestParam("id") int adviseId) {
        return fillSuccessData(standardAdviseService.downAttache(adviseId));
    }



}
